using System;
using AndyPike.Unfuddle.Commands.Projects;
using AndyPike.Unfuddle.Contracts;
using AndyPike.Unfuddle.Entities.Projects;
using NUnit.Framework;
using Rhino.Mocks;

namespace AndyPike.Unfuddle.Tests.Unit.Commands.Projects
{
    [TestFixture]
    public class When_creating_a_new_project
    {
        private MockRepository mockery;
        private IUnfuddleService service;
        private CreateProject command;
        private Project project;

        [SetUp]
        public void SetUp()
        {
            mockery = new MockRepository();
            service = mockery.DynamicMock<IUnfuddleService>();
            project = mockery.DynamicMock<Project>();
            
            command = new CreateProject(project);
        }

        [Test]
        public void Should_leverage_the_unfuddle_service()
        {
            const string url = "http://www.domain.com";

            using (mockery.Record())
            {
                Expect.Call(() => project.Validate(EntityValidationContext.Creating));
                Expect.Call(service.BuildUrl(null)).IgnoreArguments().Return(url);
                Expect.Call(service.InvokeRequest(url, HttpMethod.POST, project)).Return(null);
            }

            using (mockery.Playback())
            {
                command.Execute(service);
            }
        }

        [Test]
        [ExpectedException(typeof(ArgumentNullException))]
        public void Should_throw_an_exception_if_a_null_service_is_passed_in()
        {
            command.Execute(null);
        }
    }
}